package com.starinside.wp.service.impl;

import com.starinside.wp.foundation.exception.BusinessException;
import com.starinside.wp.foundation.model.ErrorCode;
import com.starinside.wp.repository.ChildrenRepository;
import com.starinside.wp.repository.WxUserRepository;
import com.starinside.wp.service.TeachAPICallService;
import com.starinside.wp.service.UploadService;
import com.starinside.wp.utils.RemoteCallUtil;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.Map;

@Service
public class TeachAPICallServiceImpl implements TeachAPICallService {

    @Value("${teach.server.url}")
    private String teachServerUrl;

    @Autowired
    WxUserRepository wxUserRepository;

    @Autowired
    ChildrenRepository childrenRepository;

    @Autowired
    private UploadService uploadService;

    @Override
    public Map addComplaintRecord(String wxUserId, String parentPhone, String childrenId, String complaintObject,
                                  String title, String content, String files) {

        Map param = new HashMap();

        param.put("parentPhone", parentPhone);
        param.put("childrenId", childrenId);
        param.put("complaintObject", complaintObject);
        param.put("title", title);
        param.put("content", content);
        param.put("files", files);

        return RemoteCallUtil.call(teachServerUrl + "/api/pad/complaintRecord/addRecord", param, null, Map.class, "调用 teachapi 增加投诉失败");
    }

    @Override
    public Map getComplaintRecord(String usersId, Integer page, Integer size) {
        Map param =  new HashMap();

        param.put("usersId", usersId);
        param.put("pageNum", page);
        param.put("pageSize", size);

        return RemoteCallUtil.call(teachServerUrl + "/api/pad/complaintRecord/getComplaintRecord", param, null, Map.class, "调用 teachapi 获取投诉列表失败");
    }

    @Override
    public Map addParentsRecord(String parentPhone, String childrenId, String leaveDate, int leaveType, String content){
        Map param =  new HashMap();

        param.put("parentPhone", parentPhone);
        param.put("childrenId", childrenId);
        param.put("leaveDate", leaveDate);
        param.put("leaveType", leaveType);
        param.put("content", content);

        return RemoteCallUtil.call(teachServerUrl + "/api/pad/leaveRecord/addParentsRecord", param, null, Map.class, "调用 teachapi 增加请假记录失败");
    }

    @Override
    public Map getWxLeaveRecord(String childrenId, String month,  int pageNum, int pageSize){
        Map param =  new HashMap();

        param.put("childrenId", childrenId);
        if (StringUtils.isNotEmpty(month)){
            param.put("month", month);
        }

        param.put("pageNum", pageNum);
        param.put("pageSize", pageSize);

        return RemoteCallUtil.call(teachServerUrl + "/api/pad/leaveRecord/getWxLeaveRecord", param, null, Map.class, "调用 teachapi 获取请假列表失败");
    }

    @Override
    public Map getNurseryScheduler(String classesId, String courseDateStr){
        Map param =  new HashMap();

//        http://192.168.8.238:8001
//        /api/web/nursery_schedule/get_schedule_check
//        ?classesId=2c96855b8228a66501822dabc4981018&beginDate=2023-08-28&endDate=2023-09-03
//        classesId: 2c98d81f7f4ee5ff017f4fddbad8043d
//        courseDateStr: 2023/08/29
        if (StringUtils.isAnyBlank(classesId, courseDateStr)){
            throw new BusinessException(ErrorCode.PARAM_CAN_NOT_BLANK, "参数错误");
        }

        param.put("classesId", classesId);
        param.put("courseDateStr", courseDateStr);

        return RemoteCallUtil.call(teachServerUrl + "/api/web/nursery_schedule/teacher_detail", param, null, Map.class, "调用 teachapi 获取课表失败");
    }
}
